package java01_type;

import cn.hutool.core.collection.ListUtil;
import org.junit.jupiter.api.Test;

import java.net.URLDecoder;
import java.util.Arrays;
import java.util.List;

/**
 * https://www.kancloud.cn/apachecn/beginnersbook-zh/1955443
 */
public class Test03_String {

    /**
     * 常识：工作中String得用equals比较
     */
    @Test
    public void testStringEquals() {
        String a = "String";
        String b = "String";
        String c = new String("String");
        String d = new String("String");
        System.out.println(a == b); // true
        System.out.println(a == c); // false
        System.out.println(a.equals(b)); // true
        System.out.println(a.equals(c)); // true
        System.out.println(c == d); // false
    }

    // ".$|()[{^?*+\\"
    // 上述字符，需要加上转义符\\
    @Test
    void test_split_point() {
        String s1 = "aa.bb";
        String[] split1 = s1.split("\\.");
        System.out.println(split1[0]);

        String s2 = "aa$bb";
        String[] split2 = s2.split("\\$");
        System.out.println(split2[0]);

        String s3 = "aa|bb";
        String[] split3 = s3.split("\\|");
        System.out.println(split3[0]);

        String s4 = "aa(bb";
        String[] split4 = s4.split("\\(");
        System.out.println(split4[0]);

        String s5 = "aa)bb";
        String[] split5 = s5.split("\\)");
        System.out.println(split5[0]);

        String s6 = "aa[bb";
        String[] split6 = s6.split("\\[");
        System.out.println(split6[0]);

        String s7 = "aa{bb";
        String[] split7 = s7.split("\\{");
        System.out.println(split7[0]);

        String s8 = "aa^bb";
        String[] split8 = s8.split("\\^");
        System.out.println(split8[0]);

        String s9 = "aa?bb";
        String[] split9 = s9.split("\\?");
        System.out.println(split9[0]);

        String s10 = "aa*bb";
        String[] split10 = s10.split("\\*");
        System.out.println(split10[0]);

        String s11 = "aa+bb";
        String[] split11 = s11.split("\\+");
        System.out.println(split11[0]);

        String s12 = "aa\\bb";
        String[] split12 = s12.split("\\\\");
        System.out.println(split12[0]);
    }

    /**
     * String有些方法并不会改变原来的值，而是返回新的字符串。
     * replace
     * Split
     * concat
     * toLowerCase
     * toUpperCase
     * trim
     * substring
     */

    //region 不会改变原来的值

    /**
     * replace不会改变原来的值,返回的是新的字符串
     */
    @Test
    void testReplace() {
        String a = "不变的是初心";
        System.out.println(a.replace("初心", "理想"));
        System.out.println(a);
    }

    @Test
    void testSplit() {
        String a = "不变的是初心";
        String[] split = a.split("");
        System.out.println(Arrays.toString(split));
        System.out.println(a);
    }

    /**
     * concat不会改变原来的值,返回的是新的字符串
     */
    @Test
    void testConcat() {
        String a = "不变的是初心";
        System.out.println(a.concat(" 变的是对生活的期望度"));
        System.out.println(a);
    }

    /**
     * toLowerCase 和 toUpperCase 不会修改原来字符串的值
     */
    @Test
    void testToLowerCaseAndUpperCase() {
        String song = "The Day You Went Away";    // 定义原始字符串
        System.out.println("原始字符串：" + song);
        System.out.println("使用 toLowerCase() 方法之后为：" + song.toLowerCase());
        System.out.println("使用 toUpperCase() 方法之后为：" + song.toUpperCase());
        System.out.println("song为：" + song);
    }

    /**
     * trim()不会修改原来字符串的值
     */
    @Test
    void testTrim() {
        String str = " Hello ";
        System.out.println(str.length());
        System.out.println(str.trim().length());
        System.out.println(str.length());
    }

    /**
     * substring(int beginIndex, int endIndex)，不包含endIndex
     */
    @Test
    void test_Substring() {
        String a = "0123456789.";
        //从索引位置（从0开始）开始至结尾处的字符串部分
        System.out.println(a.substring(3)); // 3456789.
        System.out.println(a.substring(3, 4)); // 3，因为substring(int beginIndex, int endIndex)，不包含endIndex
        System.out.println(a.substring(3, 5)); // 34，因为substring(int beginIndex, int endIndex)，不包含endIndex
        //System.out.println(a.substring(3, 2)); // 报异常，因为substring(int beginIndex, int endIndex)
        System.out.println(a.substring(0, a.length()));
        System.out.println(a.substring(0, a.length() - 1));
        System.out.println(a);//a的值不会改变的
    }

    //endregion

    @Test
    void testNewString() {
        String a = new String("Hello World");
        String b = new String(a);

        System.out.println(a);
        System.out.println(b);
        System.out.println(a.equals(b));

        char chars[] = {'H', 'e', 'l', 'l', 'o'};
        String sChars = new String(chars);
        System.out.println(sChars);
    }

    @Test
    void test_format() {
        String format = String.format("我是%s, Hi, %s", "亚瑟", "色王");
        System.out.println(format);
    }

    @Test
    void testString2CharArray() {
        String a = "0123";
        char[] charArray = a.toCharArray();
        System.out.println(charArray[2]);
    }

    @Test
    void testString2List() {
        String a = "1,2,3";
        List<String> list = ListUtil.toList(a.split(","));
        System.out.println(list.size());
        System.out.println(list.get(2));

        list.add("4");
        System.out.println(list.size());
    }

    @Test
    void testReverse() {
        StringBuilder sb = new StringBuilder("Hello");
        sb.reverse();
        System.out.println(sb);
    }

    @Test
    void testMultiLineString() {
        String a = """
                Hello World
                Hi
                Goodbye
                """;
        System.out.println(a.length());
        System.out.println(a.charAt(0));
    }

    @Test
    void testDecode() {
        String menuConf = "analysis:relationAnalysis|%E8%81%94%E9%80%9A%E7%BD%91%E7%BB%9C%E5%88%86%E6%9E%90,analysis:fundAnalysis|%E7%BB%8F%E6%B5%8E%E8%A1%8C%E4%B8%BA%E5%88%86%E6%9E%90,analysis:eventTimingAnalysis|%E6%97%B6%E9%97%B4%E8%BD%B4,analysis:timeSpaceTrajectoryAnalysis|%E5%9C%B0%E7%90%86%E4%BD%8D%E7%BD%AE%E5%88%86%E6%9E%90";

        String[] menuNames = menuConf.split(",");
        for (String menuName : menuNames) {
            String[] split = menuName.split("\\|");
            System.out.println(split[0]);
            System.out.println(URLDecoder.decode(split[1]));
        }
    }

    @Test
    public void testString() {
        String a = "a";
        changeString(a);
        System.out.println("testString方法里还是" + a); // a
    }

    private void changeString(String a) {
        a = "aa";
        System.out.println("changeString方法里" + a); // aa
    }

    @Test
    public void testA() {
        int i = 65;
        char a = (char) i;
        System.out.println(a);
    }

}
